package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.SecurityControl;
import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.constant.Constant;
import cc.yugu.yhdw.common.domain.basic.AlipayPayOrder;
import cc.yugu.yhdw.common.domain.basic.CustomerForegiftOrder;
import cc.yugu.yhdw.common.domain.basic.WeixinPayOrder;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.utils.HttpUtils;
import cc.yugu.yhdw.common.utils.IdUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.AlipayPayOrderService;
import cc.yugu.yhdw.webserver.service.basic.CustomerForegiftOrderService;
import cc.yugu.yhdw.webserver.service.basic.WeixinPayOrderService;
import cc.yugu.yhdw.webserver.utils.MediaUtils;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
 * 用户押金记录控制器
 * 
 * @Date:Created in 2018/7/17 14:05
 * @Author:
 * @Modified By: zjn 2018/7/17 14:05
 */ 
@Controller
@RequestMapping(value = "/security/basic/customer_foregift_order")
public class CustomerForegiftOrderController extends SecurityController {
    @Autowired
    CustomerForegiftOrderService customerForegiftOrderService;
    @Autowired
    AlipayPayOrderService alipayPayOrderService;
    @Autowired
    WeixinPayOrderService weixinPayOrderService;

    final static String UPLOAD_FILE_URL = "/security/hdw_upload/customer_foregift_order_refund_photo.htm";

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_14.getValue());
        model.addAttribute("statusEnum", CustomerForegiftOrder.Status.values());
        // 保存 申请退款 状态
        model.addAttribute("APPLY_REFUND", CustomerForegiftOrder.Status.APPLY_REFUND.getValue());
    }
    /**
    * 没有用到
    * 
    * @Param: [model, customerId]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/17  14:21
    */ 
    @RequestMapping(value = "view_foregift_order.htm")
    public String viewForegiftOrder(Model model, long customerId) {
        model.addAttribute("customerId", customerId);
        return "/security/basic/customer/view_foregift_order";
    }
    /**
    * 分页查询用户押金订单信息
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/17  14:22
    */ 
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(CustomerForegiftOrder search, HttpServletRequest request) {
//        if (search.getStatus()==null) {
//            search.setDefaultQueryStatus(CustomerForegiftOrder.Status.WAIT_PAY.getValue());
//        }else if(search.getStatus().equals(0)){
//            search.setStatus(null);
//        }
        /*
        新增加运营商代理商登录筛选
         */
        // 如果是运营商登录
        if(getSessionUser(request).getAgentId() !=0 && getSessionUser(request).getAgencyId() == 0) {
            search.setAgentId(getSessionUser(request).getAgentId());
            // 页面点击 客户押金订单查询
            if (search.getStatus() == null) {
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
            // 点击'所以'搜索 查询
            if (search.getStatus().equals(0)) {
                search.setStatus(null);
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
            // 4种付款状态查询
            if(search.getStatus() != null && !search.getStatus().equals(0)){
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
        }
        // 如果是代理商登录
        if(getSessionUser(request).getAgentId() !=0 &&getSessionUser(request).getAgencyId() != 0) {
            search.setAgentId(getSessionUser(request).getAgentId());
            search.setAgencyId(getSessionUser(request).getAgencyId());

            if (search.getStatus() == null) {

                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
            if (search.getStatus().equals(0)) {
                search.setStatus(null);
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
            if(search.getStatus() != null&&!search.getStatus().equals(0)){
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
        }
        // 如果是 总部登录
        if(getSessionUser(request).getAgentId() == 0&&getSessionUser(request).getAgencyId() == 0) {
            search.setAgentId(null);
            search.setAgencyId(null);
            if (search.getStatus() == null) {

                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
            if (search.getStatus().equals(0)) {
                search.setStatus(null);
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
            if (search.getStatus() != null && !search.getStatus().equals(0)) {
                return PageResult.successResult(customerForegiftOrderService.findPage(search));
            }
        }

        return null;
    }
    /**
    * 支付宝订单号
    * 
    * @Param: [model, id]
    * @Return: void
    * @Author By: zjn 2018/7/17  14:30
    */ 
    @RequestMapping(value = "alipay_pay_order.htm")
    public void alipayPayOrder(Model model, String id) {
        model.addAttribute("sourceId", id);
    }
    /**
    * 支付宝订单分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/17  14:31
    */ 
    @RequestMapping("alipay_pay_order_page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult alipayPayOrderPage(AlipayPayOrder search) {
        search.setSourceType(AlipayPayOrder.SourceType.FOREGIFT_ORDER_CUSTOMER_PAY.getValue());
        search.setOrderStatus(AlipayPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(alipayPayOrderService.findPage(search));
    }
    /**
    * 微信订单号
    * 
    * @Param: [model, id]
    * @Return: void
    * @Author By: zjn 2018/7/17  14:31
    */ 
    @RequestMapping(value = "weixin_pay_order.htm")
    public void weixinPayOrder(Model model, String id) {
        model.addAttribute("sourceId", id);

    }
    /**
    * 微信订单分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/17  14:31
    */ 
    @RequestMapping("weixin_pay_order_page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult weixinPayOrderPage(WeixinPayOrder search) {
        search.setSourceType(WeixinPayOrder.SourceType.FOREGIFT_ORDER_CUSTOMER_PAY.getValue());
        search.setOrderStatus(WeixinPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(weixinPayOrderService.findPage(search));
    }
    /**
    * 微信订单退款
    * 
    * @Param: [model, id]
    * @Return: void
    * @Author By: zjn 2018/7/17  14:31
    */ 
    @RequestMapping(value = "weixin_pay_order_refund.htm")
    public void weixinPayOrderRefund(Model model, String id) {
        model.addAttribute("sourceId", id);
    }
    /**
    * 微信订单退款分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/17  14:32
    */ 
    @RequestMapping("weixin_pay_order_page_refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult weixinPayOrderPageRefund(WeixinPayOrder search) {
        search.setSourceType(WeixinPayOrder.SourceType.FOREGIFT_ORDER_CUSTOMER_PAY.getValue());
        search.setOrderStatus(WeixinPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(weixinPayOrderService.findPage(search));
    }
    /**
    * 支付宝订单退款
    * 
    * @Param: [model, id]
    * @Return: void
    * @Author By: zjn 2018/7/17  14:32
    */ 
    @RequestMapping(value = "alipay_pay_order_refund.htm")
    public void alipayPayOrderRefund(Model model, String id) {
        model.addAttribute("sourceId", id);
    }
    /**
    * 支付宝订单退款信息分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/17  14:33
    */ 
    @RequestMapping("alipay_pay_order_page_refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult alipayPayOrderPageRefund(AlipayPayOrder search) {
        search.setSourceType(AlipayPayOrder.SourceType.FOREGIFT_ORDER_CUSTOMER_PAY.getValue());
        search.setOrderStatus(AlipayPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(alipayPayOrderService.findPage(search));
    }
    /**
    * 查询押金信息，前台获取显示
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/17  14:34
    */ 
    @RequestMapping(value = "view.htm")
    public String view(Model model, String id) {
        CustomerForegiftOrder entity = customerForegiftOrderService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        model.addAttribute("id", id);
        return "/security/basic/customer_foregift_order/view";
    }
    /**
    * 查询客户押金信息，在页面查看-->基本信息 中显示
    * 
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  10:44
    */ 
    @RequestMapping(value = "view_basic.htm")
    public String viewBasic(Model model, String id) {
        CustomerForegiftOrder entity = customerForegiftOrderService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
            model.addAttribute("staticUrl", appConfig.getStaticUrl());
            model.addAttribute("payTypeEnum", ConstEnum.PayType.values());
            model.addAttribute("statusEnum", CustomerForegiftOrder.Status.values());
        }
        return "/security/basic/customer_foregift_order/view_basic";
    }
    /**
    * 退还押金（目前没有用到该功能）
    *
    * @Param: [model, id]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  10:50
    */
    @RequestMapping(value = "edit_refund.htm")
    public String editRefund(Model model, String id) {
        CustomerForegiftOrder entity = customerForegiftOrderService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
            model.addAttribute("APPLY_ALIPAY", ConstEnum.PayType.ALIPAY.getValue());
            model.addAttribute("APPLY_WEIXIN", ConstEnum.PayType.WEIXIN.getValue());
        }
        return "/security/basic/customer_foregift_order/edit_refund";
    }
    /**
    * 退款
    *
    * @Param: [httpSession, customerForegiftOrder]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  10:51
    */
    @RequestMapping("refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult refund(HttpSession httpSession, CustomerForegiftOrder customerForegiftOrder) {
        SessionUser sessionUser = getSessionUser(httpSession);
        String userName = sessionUser.getUsername();
        return customerForegiftOrderService.refund(userName, customerForegiftOrder);
    }

    @RequestMapping(value = "edit_repulse_refund.htm")
    public String editRepulseRefund(Model model, String id) {
        CustomerForegiftOrder entity = customerForegiftOrderService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/basic/customer_foregift_order/edit_repulse_refund";
    }
    /**
    * 拒绝退款
    * 
    * @Param: [httpSession, customerForegiftOrder]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  10:53
    */ 
    @RequestMapping("repulse_refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult repulseRefund(HttpSession httpSession, CustomerForegiftOrder customerForegiftOrder) {
        SessionUser sessionUser = getSessionUser(httpSession);
        String userName = sessionUser.getUsername();
        return customerForegiftOrderService.repulseRefund(userName, customerForegiftOrder);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "photo_path.htm", method = RequestMethod.GET)
    public void portrait() {
    }
    /**
    * 退款凭证照片路径
    * 
    * @Param: [file, model]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/23  10:54
    */ 
    @RequestMapping(value = "photo_path.htm", method = RequestMethod.POST)
    public String portrait(@RequestParam("file") MultipartFile file, Model model) throws IOException {
        String uuid = IdUtils.uuid();
        String fileSuffix = YhdwUtils.getFileSuffix(file.getOriginalFilename());
        File sourceFile = new File(getAppConfig().tempDir, uuid + "." + fileSuffix);
        YhdwUtils.makeParentDir(sourceFile);
        file.transferTo(sourceFile);
        File targetFile = new File(sourceFile.getParentFile(), uuid +"."+fileSuffix);
        MediaUtils.MediaInfo mediaInfo = MediaUtils.getImageInfo(sourceFile);

        mediaInfo = MediaUtils.getImageSnapshotSize(mediaInfo, 800);
        MediaUtils.makeImageSnapshot(sourceFile, targetFile, fileSuffix, mediaInfo.width, mediaInfo.height);
        sourceFile.delete();
        String url = getAppConfig().staticUrl;
        HttpUtils.HttpResp httpResp = HttpUtils.uploadFile(url + UPLOAD_FILE_URL, targetFile, Collections.EMPTY_MAP); //upload to static server
        if (httpResp.status / 100 == 2) {
            Map map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
            List list = (List) map.get("data");
            Map<String, String> data = (Map<String, String>) list.get(0);
            model.addAttribute("success", true);
            model.addAttribute("filePath", data.get("filePath"));
            model.addAttribute("fileName", data.get("fileName"));
        } else {
            model.addAttribute("success", false);
            model.addAttribute("message", "上传文件出现错误");
        }

        return "/security/basic/customer_foregift_order/photo_path_response";
    }

}
